import Vue from 'vue'
import Router from 'vue-router'

// 引入主面板
import Layout from './views/layout/layout'
import config  from './config/config'

Vue.use(Router);

  
const routes = [
  { path: '/login', name: 'login', component: ()=> import('@/views/login/index') },
  { 
    path: '/',
    name: 'Layout', 
    component: Layout,
    redirect:'/home',
    children:[
      {path:'home',name:'home',component:()=>import('@/views/home/index')}
    ] 
  },
  {
    path:'/pm',
    component:Layout,
    redirect:'/pm/product',
    children:[
      {path:'product',name:'product',component:()=>import('@/views/pm/product/index')},
      {path:'addProduct',name:'add-product',component:()=>import('@/views/pm/product/add')}, 
      {path:'updateProduct',name:'update-product',component:()=>import('@/views/pm/product/update')}, 
      {path:'productCategory',name:'product-category',component:()=>import('@/views/pm/productCategory/index')},
      {path:'productCateAdd',name:'product-category-add',component:()=>import('@/views/pm/productCategory/add')},
      {path:'productCateUpdate',name:'product-category-update',component:()=>import('@/views/pm/productCategory/update')}
    ]
  },
  {
    path:'/om',
    component:Layout,
    redirect:'/om/order',
    children:[
      {path:'order',name:'order',component:()=>import('@/views/om/index')},
      {path:'setting',name:'setting',component:()=>import('@/views/om/setting')},
      {path:'deliver',name:'deliver',component:()=>import('@/views/om/deliverList')},
      {path:'detail',name:'detail',component:()=>import('@/views/om/detail')} 
    ]
  },
  {
    path:'/um',
    component:Layout,
    redirect:'/um/admin',
    children:[
      {path:'admin',name:'admin',component:()=>import('@/views/um/admin/index')},
      {path:'role',name:'role',component:()=>import('@/views/um/role/index')},
      {path:'menu',name:'menu',component:()=>import('@/views/um/menu/index')},
      {path: 'allocMenu', name: 'allocMenu', component: () => import('@/views/um/role/allocMenu')},
 
    ]
  },
  { path: '*', name: 'notFound', component: ()=> import('@/views/error') },
]

const router = new Router({
  routes
})

// 挂载路由前置守卫
router.beforeEach((to,from,next)=>{

  if(to.path === '/login') return next();
  // 获取 token
  const tokenStr = window.sessionStorage.getItem(config.MANAGET_TOKEN);
  if(!tokenStr) return next('/login');
  next();
})

export default router